//
//  JQFYChatGroupController.m
//  JQFY
//
//  Created by 代辉 on 2017/8/23.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYChatGroupController.h"
#import "JQFYChatGroupCell.h"
#import "JQFYUserJoinInChatGroupModel.h"
#import "JQFYApplicationListController.h"

@interface JQFYChatGroupController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *groups;
@end

static NSString *chatGroupCell = @"chatGroupCell";

@implementation JQFYChatGroupController

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self loadData];
}
- (void)setupUI
{
    self.navigationItem.title = @"我的群聊";
    UIImage *bgImage = [UIImage imageNamed:@"frame_top@2x(1)"];
    UIImageView *topBgImageView = [[UIImageView alloc] initWithImage:bgImage];
    [self.view addSubview:topBgImageView];
    
    [topBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgImageView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[JQFYChatGroupCell class] forCellReuseIdentifier:chatGroupCell];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 64 *SCAL;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 52 *SCAL, 0);
    
    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"广告（合并）"]];
    self.tableView.tableHeaderView = headerView;
}

- (void)loadData
{
    [[JQFYUserJoinInChatGroupModel new] getChatGroupSuccess:^(NSArray *groups) {
        self.groups = groups;
        [self.tableView reloadData];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groups.count ? self.groups.count : 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JQFYChatGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:chatGroupCell forIndexPath:indexPath];
    
    cell.groupInfo = self.groups[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JQFYApplicationListController *listVC = [JQFYApplicationListController new];
    [self.navigationController pushViewController:listVC animated:YES];
}

@end
